package model

import (
    "strings"

    commonError "git.inspur.com/x/common/error"
)

const (
    ErrorBaseCode              = "215.001"
    FunctionInvokeError        = "001"
    FunctionCreateError        = "002"
    FunctionDeleteError        = "003"
    FunctionUpdateError        = "004"
    FunctionUpdateTrafficError = "005"
    FunctionClientError        = "006"
    FunctionRequestValidError  = "007"
    FunctionGetError           = "008"
    GroupGetError              = "009"
    FunctionLogError           = "010"
    FunctionMetricsError       = "011"
    FunctionAliasError         = "012"
    TriggerError               = "016"
    FunctionUrlError           = "017"
    OrderHandleError           = "018"
    DomainError                = "019"
    LayerGetError              = "020"
)

func InternalServerError(code, message string) error {
    return commonError.InternalServerError(ErrorBaseCode+code, message)
}
func NotFoundServiceError(code, message string) error {
    return commonError.NotFoundError(ErrorBaseCode+code, message)
}

func InvalidParameterError(code, message string) error {
    return commonError.InvalidParameterError(ErrorBaseCode+code, message)
}

type Error struct {
    err         error
    errorReason string
}

const (
    NoNeedUpdateResource string = "NoNeedUpdateResource"
)

func (a Error) Error() string {
    return strings.ToLower(string(a.errorReason)) + ": " + a.err.Error()
}

// 业务错误
func NewBusinessError(err error, reason string) Error {
    return Error{
        err:         err,
        errorReason: reason,
    }
}

func IsNoNeedUpdate(err error) bool {
    businessError, ok := err.(Error)
    if !ok {
        return false
    }
    return businessError.errorReason == NoNeedUpdateResource
}
